
version 9.2

clear
set mem 300m
set more off

global foldp "C:/work/Wealth Counterfactuals/Verification/"

*Covariates for which the sample mean is estimated within each country
global varmean age hsize highs college recall badhs retire working couple widow nevmar beqex00 provhelp voluntary adlno 
*Covariates for which the sample median is estimated within each country
global varmed netinc hnetwv
*All covariates to name matrix rows (maintain the above order!!!)
global varall vars age hsize highs college recall badhs retire working couple widow nevmar beqex00 provhelp voluntary adlno netinc hnetwv


foreach cntr of numlist 0(1)12 {

   use "${foldp}Data/HRS/HRS_DG.dta", clear
   append using "${foldp}Data/SHARE/SHARE_DG.dta"
   append using "${foldp}Data/ELSA/ELSA_DG.dta"

******************************************** 
*Net income
qui gen double netinc=hgtincv-hcpincv
*Education dummies
qui gen byte nohigh=(edu==1) if edu~=.
qui gen byte highs=(edu==2) if edu~=.
qui gen byte college=(edu==3) if edu~=.
*Health status
qui gen byte badhs=(srhealtha==4 | srhealtha==5) if srhealtha~=.

*Marital Status
qui gen byte couple=(mstat==1) if mstat~=.
qui gen byte divorced=(mstat==2) if mstat~=.
qui gen byte widow=(mstat==3) if mstat~=.
qui gen byte nevmar=(mstat==4) if mstat~=.
******************************************** 

keep if country==`cntr'
keep if head==1
keep if wgtach!=0

matrix sumvar_`cntr' = .

foreach varmn of global varmean {

qui su `varmn' [aw=wgtach]
matrix sumvar_`cntr' = sumvar_`cntr'\ r(mean)
}

foreach varmd of global varmed {

qui su `varmd' [aw=wgtach], d
matrix sumvar_`cntr' = sumvar_`cntr'\ r(p50)
}

mat coln sumvar_`cntr' = `cntr'
}

matrix all_sumvar = sumvar_0

foreach cntr of numlist 1(1)12 {

matrix all_sumvar = all_sumvar, sumvar_`cntr' 
}

matrix rown all_sumvar = ${varall}



mat2txt, matrix(all_sumvar) saving(${foldp}/Results/DG/Sum_stats/Sum_vars) title(Sum Stats by Country) format(%15.2f) replace
